﻿using BIS.Portal.Core.Base;
using BIS.Portal.Core.Global;
using BIS.Portal.Modules.CMS.BO;
using BIS.Portal.Modules.CMS.Entity;
using BIS.Utility.Converter;
using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;

namespace BIS.Portal.Modules.CMS
{
    public partial class CmsAdvanceSearch : ControlBase
    {

        protected void btnTim_Kiem_Click(object sender, EventArgs e)
        {
            int iArticle_Num = Functions.GetInt(this.SoLuongTin);
            DateTime tu_ngay = Convert.ToDateTime("01/02/1888");
            DateTime den_ngay = Convert.ToDateTime("01/02/4999");
            string urlAdvanceSearchKeyword = WebHelpers.GetLink(ModuleQueryStringKey.UrlAdvancedSearchNoKeyword, PortalContext.PageAlias,
                 new string[] { ModuleQueryStringKey.cms_from_date, ModuleQueryStringKey.cms_to_date, ModuleQueryStringKey.CategoryId },
                new string[] { tu_ngay.ToString("yyyy-MM-dd"), den_ngay.ToString("yyyy-MM-dd"), Loai_Tin.SelectedValue }
                );
            string urlAdvanceSearchKeywordPaging = ModuleQueryStringKey.UrlAdvancedSearchNoKeywordWithPaging;

            if ((this.Tu_Ngay.Text != "") && (this.Den_Ngay.Text != ""))
            {
                tu_ngay = Convert.ToDateTime(this.Tu_Ngay.Text);
                den_ngay = Convert.ToDateTime(this.Den_Ngay.Text);
            }
            else if (this.Tu_Ngay.Text != "")
            {
                tu_ngay = Convert.ToDateTime(this.Tu_Ngay.Text);
            }
            else if (this.Den_Ngay.Text != "")
            {
                den_ngay = Convert.ToDateTime(this.Den_Ngay.Text);
            }

            if (txtTu_Khoa.Text != "")
            {
                urlAdvanceSearchKeyword = WebHelpers.GetLink(ModuleQueryStringKey.UrlAdvancedSearchWithKeyword, PortalContext.PageAlias,
                 new string[] {ModuleQueryStringKey.cms_keyword,ModuleQueryStringKey.cms_from_date, ModuleQueryStringKey.cms_to_date, ModuleQueryStringKey.CategoryId },
                new string[] {txtTu_Khoa.Text,tu_ngay.ToString("yyyy-MM-dd"), den_ngay.ToString("yyyy-MM-dd"), Loai_Tin.SelectedValue }
                );
                urlAdvanceSearchKeywordPaging = ModuleQueryStringKey.UrlAdvancedSearchWithKeywordAndPaging;
            }
            Session["pagingAdvance"] = urlAdvanceSearchKeywordPaging;
            Response.Redirect(urlAdvanceSearchKeyword);
        }

        private string GetCateName(int iCateId)
        {
            return ClientCmsArticleCategoryAndCategoryBO.GetCategoryNameById(iCateId).Category_name;
        }

        private void load_paging()
        {
            string keyword =Functions.GetString(Request[ModuleQueryStringKey.cms_keyword]);
            DateTime tu_ngay = Convert.ToDateTime("01/02/1888");
            DateTime den_ngay = Convert.ToDateTime("01/02/4999");
            int pageIndex = 0;
            #region Get data tu querystring
            if (Request[ModuleQueryStringKey.cms_from_date] != null)
            {
                tu_ngay = Functions.GetDateTime(Request[ModuleQueryStringKey.cms_from_date]);
            }
            if (Request[ModuleQueryStringKey.cms_to_date] != null)
            {
                den_ngay = Functions.GetDateTime(Request[ModuleQueryStringKey.cms_to_date]);
            }
            int cateId = Functions.GetInt(Request[ModuleQueryStringKey.CategoryId]);

            
            if (Request[ModuleQueryStringKey.PageIndex] != null)
            {
                pageIndex = Functions.GetInt(Request[ModuleQueryStringKey.PageIndex]) - 1;
            }
            
            #endregion

            #region Bind dữ liệu tin tức
            IList<ClientCmsArticleAndCategoryEntity> list = ClientCmsArticleBO.GetArticleAdvanceSearch(keyword, tu_ngay, den_ngay, PortalContext.LanguageInUseId, pageIndex, Functions.GetInt(this.SoLuongTin), cateId);
            
            if (list.Count > 0)
            {
                this.Tin_Tuc.DataSource = list;
                this.Tin_Tuc.DataBind();
                string urlPaging = (string)Session["pagingAdvance"];
                this.PagingControl.QueryStringName = ModuleQueryStringKey.PageIndex;
                this.PagingControl.UrlRewriteRule = urlPaging;
                this.PagingControl.TotalRecord = ClientCmsArticleBO.ArticleCount;
                this.PagingControl.RecordPerPage = Functions.GetInt(this.SoLuongTin);
                this.PagingControl.RefreshPaging();
            }
            #endregion
            
            #region Bind data form search

            txtTu_Khoa.Text = keyword;
            Tu_Ngay.Text = tu_ngay.ToString("MM/dd/yyyy");
            Den_Ngay.Text = den_ngay.ToString("MM/dd/yyyy");
            Loai_Tin.SelectedValue = Functions.GetString(cateId);

            #endregion
        }
        #region Load data dropdown list
        private void loadDataDropdownList(int parent_id, string sSeparator, ref IList<ClientCmsMenuEnity> list)
        {
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].ParentCategoryId == parent_id)
                {
                    ListItem item = new ListItem(sSeparator + list[i].CategoryName, Functions.GetString(list[i].CategoryId));
                    this.Loai_Tin.Items.Add(item);
                    this.loadDataDropdownList(list[i].CategoryId, "..." + sSeparator, ref list);
                }
            }
        }
        #endregion

        private void loaddataNhan()
        {
            this.Nhan_Tu_Khoa.Text = base.GetLanguageValue("lblTuKhoa");
            this.Nhan_Tu_Ngay.Text = base.GetLanguageValue("lblTuNgay");
            this.Nhan_Den_Ngay.Text = base.GetLanguageValue("lblDenNgay");
            this.btnTim_Kiem.Text = base.GetLanguageValue("btnSearch");
            this.Nhan_Loai_Tin.Text = base.GetLanguageValue("lblLoaiTin");
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.Loai_Tin.Items.Clear();
            ListItem item = new ListItem("---Chọn Loại Tin-----", "0");
            this.Loai_Tin.Items.Add(item);
            IList<ClientCmsMenuEnity> list = ClientCmsArticleCategoryAndCategoryBO.GetCategoryForLanguage(PortalContext.LanguageInUseId);
            this.loadDataDropdownList(0, "", ref list);
            this.loaddataNhan();
            this.load_paging();
        }

        protected void Tin_Tuc_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                ClientCmsArticleAndCategoryEntity objArticle = (ClientCmsArticleAndCategoryEntity)e.Item.DataItem;
                if (null != objArticle)
                {
                    #region URL link
                    string urlChiTiet = WebHelpers.GetLink("Cms_ArticleDetail", objArticle.Page_alias, new string[] { "cms_category_id", "article_id", "cms_category_name", "article_title" }, new string[] { Convert.ToString(objArticle.Category_id), Convert.ToString(objArticle.Article_id), Functions.UnicodeToKoDauAndGach(this.GetCateName(objArticle.Category_id)), Functions.UnicodeToKoDauAndGach(objArticle.Article_title) });
                    if (this.HienThiAnh)
                    {
                        Image imgAnh = e.Item.FindControl("Anh_Minh_Hoa") as Image;
                        if (imgAnh != null)
                        {
                            imgAnh.ImageUrl = objArticle.Thumb_image;
                            imgAnh.AlternateText = objArticle.Article_title;
                            imgAnh.Attributes.Add("onerror", "ImageNotFound(this);");
                        }
                    }
                    #endregion

                    #region Bind link tieu de
                    HyperLink lnkTieuDe = e.Item.FindControl("Tieu_De_Tin") as HyperLink;
                    if (lnkTieuDe != null)
                    {
                        lnkTieuDe.ToolTip = objArticle.Article_title;
                        lnkTieuDe.NavigateUrl = urlChiTiet;
                        lnkTieuDe.Text = objArticle.Article_title;
                    }
                    #endregion

                    #region Bind data Hien thi mo ta
                    if (this.HienThiMoTa)
                    {
                        Literal ltrNoiDung = e.Item.FindControl("Mo_Ta") as Literal;
                        if (ltrNoiDung != null)
                        {
                            ltrNoiDung.Text = objArticle.Article_description;
                        }
                    }
                    #endregion

                    #region Bind data Link Ct
                    if (this.HienThiLinkXemCT)
                    {
                        HyperLink lnkXemTiep = e.Item.FindControl("Link_Xem_CT") as HyperLink;
                        if (lnkXemTiep != null)
                        {
                            lnkXemTiep.Text = base.GetLanguageValue("ReadMoreButton");
                            lnkXemTiep.NavigateUrl = urlChiTiet;
                        }
                    }
                    #endregion

                }
            }
        }

        #region Load data settings
        public bool HienThiAnh
        {
            get
            {
                return base.GetControlSetting<bool>(ModuleSettingNames.CmsAdvanceSearch.ddlAnhMinhHoa, PortalContext.LanguageAlias);
            }
        }

        public bool HienThiLinkXemCT
        {
            get
            {
                return base.GetControlSetting<bool>(ModuleSettingNames.CmsAdvanceSearch.ddlLinkCT, PortalContext.LanguageAlias);
            }
        }

        public bool HienThiMoTa
        {
            get
            {
                return base.GetControlSetting<bool>(ModuleSettingNames.CmsAdvanceSearch.ddlMoTa, PortalContext.LanguageAlias);
            }
        }

        public string SoLuongTin
        {
            get
            {
                return base.GetControlSetting<string>(ModuleSettingNames.CmsAdvanceSearch.txtSLTin, PortalContext.LanguageAlias);
            }
        }
        #endregion

    }
}

